<template>
  <div>
    <el-table
      :data="state.tableData"
      class="customer-table"
      max-height="250"
      v-loading="state.loading"
      :element-loading-background="
        themeType === 'night'
          ? 'rgba(0, 0, 0, 0.6)'
          : 'rgba(255, 255, 255, 0.6)'
      "
      header-cell-class-name="eltablebg"
      style="width: 100%"
    >
      <el-table-column
        prop
        :label="$t('exchange.lever')"
        label-class-name="left-radius-8"
      >
        <template #default="r">
          <span>
            {{
              r.row.currency_name +
                "/" +
                r.row.legal_name +
                " x" +
                r.row.lever_multiple
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="lever_open_price"
        :label="$t('exchange.openingPrice')"
      >
        <template #default="r">
          {{ $currency(r.row.lever_open_price) }}
        </template>
      </el-table-column>
      <el-table-column prop="lever_earnest_money" :label="$t('exchange.margin')"
        ><template #default="r">
          {{ r.row.lever_earnest_money }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="lever_hand"
        width="150"
        :label="$t('exchange.lot')"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="lever_type"
        :label="$t('exchange.type')"
      >
        <template #default="r">
          <span :class="r.row.lever_type === 1 ? 'text-green' : 'text-red'">
            {{
              r.row.lever_type === 1
                ? $t("exchange.doMore")
                : $t("exchange.short")
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop :label="$t('deal.stoploss')">
        <template #default="r">
          <span>{{
            (r.row.stop_win_price ? $currency(r.row.stop_win_price) : "--") +
              "/" +
              (r.row.stop_lose_price ? $currency(r.row.stop_lose_price) : "--")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="lever_open_time"
        :label="$t('lever.opentime')"
      >
        <template #default="r">
          <span>{{ getTimeZoneDate(r.row.lever_open_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="lever_overnight_fee"
        :label="$t('exchange.overnightFee')"
        ><template #default>
          <span>--</span>
        </template></el-table-column
      >
      <el-table-column
        align="center"
        prop="lever_trigger_price"
        :label="$t('deal.triggerprice')"
      ></el-table-column>
      <el-table-column
        align="right"
        prop="address"
        :label="$t('assets.operate')"
        label-class-name="right-radius-8"
      >
        <template #default="r">
          <div>
            <el-button type="text" class="free-box" @click="winloseStop(r.row)">
              {{ $t("exchange.stopLoss") }}
            </el-button>
            <el-button type="text" @click="close(r.row.id)">
              {{ $t("exchange.cancelOrder") }}
            </el-button>
          </div>
        </template>
      </el-table-column>
      <template #empty>
        <div>
          <Norecord :useDark="true" />
          <!-- <div class="quotet3 text-12 mb-60 l-10">暂无数据</div> -->
        </div>
      </template>
    </el-table>
    <!--撤单  -->
    <Messagebox
      :dialog="state.dialog"
      :text="$t('deal.cancelorder')"
      @onconfirm="confirm"
      :type="3"
      @oncancer="state.dialog = false"
    />
    <!-- 监听table滚动条触底 -->
    <Tablescroll @scroll="tableScroll" />
  </div>
</template>

<script>
import Norecord from "@/components/noRecord/NoRecord.vue";
import { reactive, onMounted, watch, computed } from "vue";
import { leverPage, cancelOrderLever } from "@/api/currency";
import { democancelOrderLever, demoleverPage } from "@/api/demo";
import UseSystemSetting from "@/commonapi/useSystemSetting";
import Messagebox from "@/components/messageBox/index.vue";
import { ElMessage } from "element-plus";
import Tablescroll from "@/components/tablescroll/index.vue";
import { useStore } from "vuex";
import { LoginInterception } from "@/utils/common";
import { getTimeZoneDate } from "@/utils/times.js";

export default {
  name: "Lever-Curent",
  components: { Norecord, Messagebox, Tablescroll },
  emits: ["winlose"],
  props: { checked: Boolean, tradingid: Number },
  setup(props, { emit }) {
    const $store = useStore();
    const state = reactive({
      listParam: {
        page: 0,
        limit: 10,
        lever_status: 1,
      },
      loading: false,
      tableData: [],
      orderId: "", //撤单id
      dialog: false,
      total: 0,
    });
    /**
     * methods
     */
    onMounted(() => {
      getList();
    });
    // 获取开关状态
    const { virtual_wallet } = UseSystemSetting();
    // eslint-disable-next-line no-unused-vars
    watch(props, (val) => {
      state.listParam.page = 0;
      getList();
    });
    const getorderType = computed(() => {
      return $store.state.exchange.leverOrder_type;
    });
    const themeType = computed(() => {
      return $store.state.exchange.theme;
    });
    watch(
      () => [getorderType.value, virtual_wallet.value],
      () => {
        if ($store.state.user?.token) {
          state.listParam.page = 0;
          getList();
        }
      }
    );
    const getList = () => {
      const param = { ...state.listParam };
      // 判断是否只显示当前币
      // if (props.checked) {

      // }
      param.currency_match_id = props.tradingid;
      state.loading = true;
      LoginInterception("/lever")
        .then(() => {
          let setFun = virtual_wallet.value ? demoleverPage : leverPage;
          setFun(param)
            .then((res) => {
              state.loading = false;
              if (state.listParam.page === 0) {
                state.tableData = res.data.items;
              } else {
                state.tableData = state.tableData.concat(res.data.items);
              }
              state.total = res.data.total;
            })
            .catch(() => {
              state.loading = false;
            });
        })
        .catch(() => {
          state.loading = false;
        });
    };
    // 止盈止损
    const winloseStop = (item) => {
      emit("winlose", item);
    };
    // 撤单
    const close = (id) => {
      state.orderId = id;
      state.dialog = true;
    };

    const confirm = () => {
      let setFun = virtual_wallet.value
        ? democancelOrderLever
        : cancelOrderLever;
      setFun(state.orderId).then((res) => {
        if (res.code === 200) {
          state.dialog = false;
          ElMessage({
            type: "success",
            message: res.message,
          });
          state.listParam.page = 0;
          getList();
        }
      });
    };
    // table滚动条触底
    const tableScroll = () => {
      if (state.total > state.tableData.length) {
        state.listParam.page = state.listParam.page + 1;
        getList();
      }
    };
    return {
      state,
      getList,
      winloseStop,
      confirm,
      close,
      tableScroll,
      getTimeZoneDate,
      themeType,
    };
  },
};
</script>

<style scoped lang="less"></style>
